<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>管理分区</title>
		<!-- 导入jquery核心类库 -->
		<script type="text/javascript" src="../../js/jquery-1.8.3.js"></script>
		<!--导入ocupload-->
		<script type="text/javascript" src="../../js/ocupload/jquery.ocupload-1.1.2.js"></script>
		<!-- 导入easyui类库 -->
		<link rel="stylesheet" type="text/css" href="../../js/easyui/themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../js/easyui/themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../js/easyui/ext/portal.css">
		<link rel="stylesheet" type="text/css" href="../../css/default.css">
		<script type="text/javascript" src="../../js/easyui/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../js/easyui/ext/jquery.portal.js"></script>
		<script type="text/javascript" src="../../js/easyui/ext/jquery.cookie.js"></script>
		<script src="../../js/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
		<script type="text/javascript">
			$.fn.serializeJson = function() {
				var serializeObj = {};
				var array = this.serializeArray();
				var str = this.serialize();
				$(array).each(function() {
					if(serializeObj[this.name]) {
						if($.isArray(serializeObj[this.name])) {
							serializeObj[this.name].push(this.value);
						} else {
							serializeObj[this.name] = [serializeObj[this.name], this.value];
						}
					} else {
						serializeObj[this.name] = this.value;
					}
				});
				return serializeObj;
			};
		
			function doAdd() {
				$('#addWindow').window("open");
			}

			function doEdit() {
				var rows = $("#grid").datagrid("getSelections");
				if(rows.length==1){
					$("#subAreaForm").form('load',rows[0]);
					
					$("#fixedAreaId").combobox({
						onLoadSuccess: function(){
							$('#fixedAreaId').combobox('setValue', rows[0].fixedArea.id);
						}
					});
					
					$("#areaId").combobox({
						onLoadSuccess:function(){
							$('#areaId').combobox('setValue', rows[0].area.id);
						}
					});
					
//					$("#province").combobox({
//						onLoadSuccess:function(){
//							$("#province").combobox('setValue',rows[0].area.province);
//						}
//					});
//					$("#city").combobox({
//						onLoadSuccess:function(){
//							$("#city").combobox('setValue',rows[0].area.city);
//						}
//					});
//					$("#district").combobox({
//						onLoadSuccess:function(){
//							$("#district").combobox('setValue',rows[0].area.district);
//						}
//					});
					
					$("#addWindow").window("open");
				}else{
					$.messager.alert('警告','请选择一条分区信息进行修改','warning');					
				}
			}

			function doDelete() {
				//获取要删除的ID
				var rows = $("#grid").datagrid('getSelections');
				if(rows.length == 0) {
					$.messager.alert('警告', '请至少选择一条数据进行删除', 'warning');
				} else {
					$.messager.confirm('确认', '真的要删除这些数据吗？', function(r) {
						if(r) {
							//获取的id添加到数组中
							var array = new Array();
							for(var i = 0; i < rows.length; i++) {
								array.push(rows[i].id);
							}
							var ids = array.join(",");
							window.location.href = "../../subArea_delBatch.action?ids=" + ids;
						}
					});
				}
			}

			function doSearch() {
				$('#searchWindow').window("open");
			}


			//工具栏
			var toolbar = [{
				id: 'button-search',
				text: '查询',
				iconCls: 'icon-search',
				handler: doSearch
			}, {
				id: 'button-add',
				text: '增加',
				iconCls: 'icon-add',
				handler: doAdd
			}, {
				id: 'button-edit',
				text: '修改',
				iconCls: 'icon-edit',
				handler: doEdit
			}, {
				id: 'button-delete',
				text: '删除',
				iconCls: 'icon-cancel',
				handler: doDelete
			}, {
				id: 'button-import',
				text: '导入',
				iconCls: 'icon-redo'
			}, {
				id: 'button-export',
				text: '导出',
				iconCls: 'icon-undo',
				handler:function(){
					window.location.href="../../subArea_batchExport.action"
				}
			}];
			// 定义列
			var columns = [
				[{
					field: 'id',
					checkbox: true,
				}, {
					field: 'showid',
					title: '分区编号',
					width: 120,
					align: 'center',
					formatter: function(data, row, index) {
						return row.id;
					}
				}, {
					field: 'area.province',
					title: '省',
					width: 40,
					align: 'center',
					formatter: function(data, row, index) {
						if(row.area != null) {
							return row.area.province;
						}
						return "";
					}
				}, {
					field: 'area.city',
					title: '市',
					width: 40,
					align: 'center',
					formatter: function(data, row, index) {
						if(row.area != null) {
							return row.area.city;
						}
						return "";
					}
				}, {
					field: 'area.district',
					title: '区',
					width: 40,
					align: 'center',
					formatter: function(data, row, index) {
						if(row.area != null) {
							return row.area.district;
						}
						return "";
					}
				}, {
					field: 'keyWords',
					title: '关键字',
					width: 120,
					align: 'center'
				}, {
					field: 'startNum',
					title: '起始号',
					width: 100,
					align: 'center'
				}, {
					field: 'endNum',
					title: '终止号',
					width: 100,
					align: 'center'
				}, {
					field: 'single',
					title: '单双号',
					width: 100,
					align: 'center'
				}, {
					field: 'assistKeyWords',
					title: '辅助关键字',
					width: 100,
					align: 'center'
				}]
			];

			$(function() {
				// 先将body隐藏，再显示，不会出现页面刷新效果
				$("body").css({
					visibility: "visible"
				});

				// 分区管理数据表格
				$('#grid').datagrid({
					iconCls: 'icon-forward',
					fit: true,
					border: true,
					rownumbers: true,
					striped: true,
					pageList: [30, 50, 100],
					pagination: true,
					toolbar: toolbar,
					url: "../../subArea_pageQuery.action",
					idField: 'id',
					columns: columns,
					onDblClickRow: doDblClickRow
				});

				// 添加、修改分区
				$('#addWindow').window({
					title: '添加修改分区',
					width: 600,
					modal: true,
					shadow: true,
					closed: true,
					height: 400,
					resizable: false
				});

				// 查询分区
				$('#searchWindow').window({
					title: '查询分区',
					width: 400,
					modal: true,
					shadow: true,
					closed: true,
					height: 400,
					resizable: false
				});


				//添加与修改保存事件
				$("#save").click(function(){
					if($("#subAreaForm").form('validate')){
						$("#subAreaForm").submit();
					}else{
						$.messager.alert('警告','表单中存在非法内容','warning');
					}
				});
				
				//查询按钮，进行click事件
				$("#searchBtn").click(function(){
					var params = $("#searchForm").serializeJson();
					$("#grid").datagrid('load',params);
					$('#searchWindow').window('close');
				});
				
				
				//一键导入
				 //为导入按钮，添加一键上传效果
			    $("#button-import").upload({
			    	
			    	//默认name为file
			    	name:'upload',//上传文件的名字
			    	action:'../../subArea_batchImport.action',
			    	//限制添加文件的类型
			    	onSelect:function (self, element) {  
                        this.autoSubmit = false;  
                        var reg = /^.*\.(xls|xlsx)$/;  
//    					alert(this.filename());
                        if (!reg.test(this.filename())) {  
//                          alert("只能上传xls或者xlsx结尾的文件");  
							$.messager.alert("警告","只能上传xls或者xlsx结尾的文件","warning");
                        }  
                        else {  
                            this.submit();  
                        }  
                    },  
			    	onComplete:function(response){
			    		$.messager.alert("提示","文件上传成功","info");
			    	}
			    });
				
				
				$("#province").combobox({
					valueField:'province',
					textField:'province',
					url:'../../queryProvince.action',
					onSelect:function(record){
						$.post("../../queryCity.action",{"province":record.province},function(data){
							$("#city").combobox({
								valueField:'city',
								textField:'city',
								onSelect:function(re){
									$.post("../../queryDistrict.action",{"city":re.city},function(da){
										$("#district").combobox({
											valueField:'id',
											textField:'district'
										});
										$("#district").combobox('loadData',da);
									});
								}
							});
							$("#city").combobox('loadData',data);
						});
					}
				});
				
			});

			function doDblClickRow() {
				alert("双击表格数据...");
			}
		</script>
	</head>

	<body class="easyui-layout" style="visibility:hidden;">
		<div region="center" border="false">
			<table id="grid"></table>
		</div>
		<!-- 添加 修改分区 -->
		<div class="easyui-window" title="分区添加修改" id="addWindow" collapsible="false" minimizable="false" maximizable="false" style="top:20px;left:200px">
			<div style="height:31px;overflow:hidden;" split="false" border="false">
				<div class="datagrid-toolbar">
					<a id="save" icon="icon-save" href="#" class="easyui-linkbutton" plain="true">保存</a>
				</div>
			</div>

			<div style="overflow:auto;padding:5px;" border="false">
				<form id="subAreaForm" action="../../subArea_save.action" method="post">
					<table class="table-edit" width="80%" align="center">
						<tr class="title">
							<td colspan="2">分区信息</td>
						</tr>
						<tr>
							<td>分区编码</td>
							<td>
								<input type="text" name="id" class="easyui-validatebox" required="true" />
							</td>
						</tr>
					
								
						
						<tr>
							<td>选择区域</td>
							<td>
								<!--<input class="easyui-combobox" id="areaId" name="area.id" data-options="required:true,valueField:'id',textField:'info',url:'../../findAllArea.action'" />-->
								<input class="easyui-combobox" id="province" value="---请选择省份---" data-options="required:true" /><br />
								<input class="easyui-combobox" id="city"  value="---请选择市区---" data-options="required:true" />	<br />				
								<input class="easyui-combobox" id="district" name="area.id" value="---请选择区(县)---" data-options="required:true" />
							</td>
						</tr>
						<tr>
							<td>选择定区</td>
							<td>
								<input class="easyui-combobox" id="fixedAreaId" name="fixedArea.id" data-options="required:true,valueField:'id',textField:'fixedAreaName',url:'../../findAllFixedArea.action'" />
							</td>
						</tr>
						<tr>
							<td>关键字</td>
							<td>
								<input type="text" name="keyWords" class="easyui-validatebox" required="true" />
							</td>
						</tr>
						<tr>
							<td>起始号</td>
							<td>
								<input type="text" name="startNum" class="easyui-validatebox" required="true" />
							</td>
						</tr>
						<tr>
							<td>终止号</td>
							<td>
								<input type="text" name="endNum" class="easyui-validatebox" required="true" />
							</td>
						</tr>
						<tr>
							<td>单双号</td>
							<td>
								<select class="easyui-combobox" name="single" style="width:150px;">
									<option value="0">单双号</option>
									<option value="1">单号</option>
									<option value="2">双号</option>
								</select>
							</td>
						</tr>
						<tr>
							<td>辅助关键字</td>
							<td>
								<input type="text" name="assistKeyWords" class="easyui-validatebox" required="true" style="width:250px;" />
							</td>
						</tr>
					</table>
				</form>
			</div>
		</div>
		<!-- 查询分区 -->
		<div class="easyui-window" title="查询分区窗口" id="searchWindow" collapsible="false" minimizable="false" maximizable="false" style="top:20px;left:200px">
			<div style="overflow:auto;padding:5px;" border="false">
				<form id="searchForm">
					<table class="table-edit" width="80%" align="center">
						<tr class="title">
							<td colspan="2">查询条件</td>
						</tr>
						<tr>
							<td>省</td>
							<td>
								<input type="text" name="area.province" class="easyui-validatebox"  />
							</td>
						</tr>
						<tr>
							<td>市</td>
							<td>
								<input type="text" name="area.city" class="easyui-validatebox"  />
							</td>
						</tr>
						<tr>
							<td>区（县）</td>
							<td>
								<input type="text" name="area.district" class="easyui-validatebox"  />
							</td>
						</tr>
						<tr>
							<td>定区编码</td>
							<td>
								<input type="text" name="fixedArea.id" class="easyui-validatebox"  />
							</td>
						</tr>
						<tr>
							<td>关键字</td>
							<td>
								<input type="text" name="keyWords" class="easyui-validatebox"  />
							</td>
						</tr>
						<tr>
							<td colspan="2">
								<a id="searchBtn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</a>
							</td>
						</tr>
					</table>
				</form>
			</div>
		</div>
	</body>

</html>